 /**
 * DISCLIMER: 
 * this library is an opensource, feel free to edit or redistribute it
 * but use it with you own risk
 * 
 * ktutnik
 */
package org.ticlib.navigator
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.net.SharedObject;
	
	public class PresistentGlobals extends EventDispatcher implements IUserManager
	{
		protected var sharedObject:SharedObject;
		
		public function PresistentGlobals(target:IEventDispatcher=null)
		{
			super(target);
			sharedObject = SharedObject.getLocal("__user_auth");
		}
		
		//------------------------ properties
		[Bindable("authChange")]
		public function get isAuthentic():Boolean
		{
			return sharedObject.data.isAuthentic;
		}
		
		public function set isAuthentic(name:Boolean):void
		{
			sharedObject.data.isAuthentic = name;
			dispatchEvent(new Event("authChange"));
		}
		
		[Bindable("displayNameChange")]
		public function get displayName():String
		{
			return sharedObject.data.displayName;
		}
		
		public function set displayName(name:String):void
		{
			sharedObject.data.displayName = name;
			dispatchEvent(new Event("displayNameChange"));
		}
		
		[Bindable("emailChange")]
		public function get email():String
		{
			return sharedObject.data.email;
		}
		
		public function set email(name:String):void
		{
			sharedObject.data.email = name;
			dispatchEvent(new Event("emailChange"));
		}
		
		[Bindable("authChange")]
		public function get userName():String
		{
			return sharedObject.data.userName;
		}
		
		public function set userName(name:String):void
		{
			sharedObject.data.userName = name;
			dispatchEvent(new Event("authChange"));
		}
		
		[Bindable("authChange")]
		public function get roles():Array
		{
			return sharedObject.data.roles;
		}
		
		public function set roles(roles:Array):void
		{
			sharedObject.data.roles = roles;
			dispatchEvent(new Event("authChange"));
		}
		
		[Bindable("passwordChange")]
		public function get password():String
		{
			return sharedObject.data.password;
		}
		
		public function set password(value:String):void
		{
			sharedObject.data.password = value;
			dispatchEvent(new Event("passwordChange"));
		}
		
		[Bindable("keepLoginChange")]
		public function get keepLogin():Boolean
		{
			return sharedObject.data.keepLogin;
		}
		
		public function set keepLogin(val:Boolean):void
		{
			sharedObject.data.keepLogin = val;
			dispatchEvent(new Event("keepLoginChange"));
		}
		
		private var _currentPage:String;
		
		[Bindable("currentPageChange")]
		public function get currentPage():String
		{
			return _currentPage;
		}
		
		public function set currentPage(val:String):void
		{
			_currentPage = val;
			dispatchEvent(new Event("currentPageChange"));
		}

		private var _version:String;
		
		[Bindable("versionChange")]
		public function get version():String
		{
			return _version;
		}
		
		public function set version(val:String):void
		{
			_version = val;
			dispatchEvent(new Event("versionChange"));
		}

		//-------------------- functions
		public function authorize(username:String, roles:Array):void
		{
			this.userName = username;
			this.roles = roles;
			isAuthentic = true;
		}
		
		public function canAccess(roles:Array):Boolean
		{
			for each(var cRole:String in roles)
			{
				for each(var uRole:String in this.roles)
				{
					if(cRole == uRole) return true;
				}
			}
			return false;
		}

		public function clear():void
		{
			isAuthentic = false;
			userName = "";
			password = "";
			email = "";
			roles = null;
			keepLogin = false;
		}
	}
}